package org.xteam.box2text.old;

import java.util.Arrays;
import java.util.List;

import org.xteam.box2text.Frame;
import org.xteam.box2text.Line;
import org.xteam.box2text.Point;
import org.xteam.box2text.Text;
import org.xteam.box2text.TextStruct;


public class TextBox extends Box implements IPlaceableBox {
	
	private Line contents;

	public TextBox(String contents) {
		this(new Line(contents));
	}
	
	public TextBox(Line contents) {
		this.contents = contents;
	}

	public boolean isEmpty() {
		return contents.width() == 0;
	}

	public Line contents() {
		return contents;
	}

	public int width(Frame frame) {
		return contents.width();
	}

	public TextStruct place(Frame frame, TextStruct textStruct, int context) {
		if (frame.y() != textStruct.y()) {
			textStruct = new TextStruct(textStruct.text().startNewLine(), new Point(0, textStruct.y() + 1));
		}
		Text t = textStruct.text().addToLastLine(new Line(frame.x() - textStruct.x()).concat(contents));
		return new TextStruct(t, new Point(frame.x() + contents.width(), frame.y()));
	}
	
	public Point length(Frame frame, int context) {
		return new Point(contents.width(), 0);
	}
	
	@Override
	public List<Box> normalizeGroups(boolean splice) {
		return Arrays.asList(new Box[] { this });
	}
	
	public String toString() {
		return "\"" + contents + "\"";
	}

}
